﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ArduinoGenericMagneticFieldSensor
{
    public class TaskSensorCollect: TasksLibrary.Tasks.Task
    {
        public TaskSensorCollect(string Name)
            : base(Name)
        {
            AddMessage<Arduino.Messages.Proxy.AnalogOutputUpdate>(15);//, new TasksLibrary.EventTaskMessageAdding<Arduino.Messages.Proxy.AnalogOutputUpdate>(TaskMessageHandler));
            IsPeriodicTask = true;
        }

        //private bool TaskMessageHandler(TasksLibrary.TaskMessage TaskMgr, Arduino.Messages.Proxy.AnalogOutputUpdate Message)
        //{
        //    return true;
        //}

        public int Value
        {
            get
            {
                int val = 0;

                var tskmessage = GetMessage<Arduino.Messages.Proxy.AnalogOutputUpdate>();
                if (tskmessage != null && tskmessage.MessagePointer > 0)
                {
                    for (int i = 0; i < tskmessage.MessagePointer; i++)
                        val += tskmessage.Get<Arduino.Messages.Proxy.AnalogOutputUpdate>(i).Body.Value;
                    val = (int)((float)val / tskmessage.MessagePointer);
                }

                return val;
            }
        }
    }
}
